package org.Lin.util;

import java.io.*;
import java.nio.file.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileUtil {

    private static final String LOG_DIR = "logs/";

    static {
        try {
            Files.createDirectories(Paths.get(LOG_DIR));
        } catch (IOException e) {
            throw new RuntimeException("无法创建日志目录: " + LOG_DIR, e);
        }
    }

    public static void storeReceivedData(String data) {
        try {
            String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
            Path filePath = Paths.get(LOG_DIR + timestamp + ".json");
            Files.write(filePath, data.getBytes(), StandardOpenOption.CREATE_NEW);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void appendToFile(String fileName, String content) {
        try {
            Path path = Paths.get(LOG_DIR + fileName);
            Files.write(path, (content + "\n").getBytes(), StandardOpenOption.APPEND, StandardOpenOption.CREATE);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
